package com.hongsuan.pplive.mobileAPI.file;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cnjson.common.config.Global;
import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.mobileAPI.utils.UploadFileUtils;
import com.hongsuan.pplive.mobileAPI.vo.ReturnObject;
import com.hongsuan.pplive.mobileAPI.vo.UploadFile;
import com.hongsuan.pplive.modules.sys.entity.User;
import com.hongsuan.pplive.modules.sys.service.SystemService;

@Controller
@RequestMapping("/v2/file")
public class UploadFileController extends BaseController {

	@Autowired
	private SystemService systemService;

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody String upload(@RequestParam("userId") String userId, HttpServletRequest request) {
		ReturnObject ret = new ReturnObject();
		try {
			User user = systemService.getUser(userId);
			if (user == null) {
				ret.setCode(1);
				ret.setMessage("The user id is not exist!");
				return ret.toString();
			}
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile mf = multipartRequest.getFile("file");
			if (!UploadFileUtils.isPicture(mf.getOriginalFilename())) {
				ret.setCode(2);
				ret.setMessage("File format is not correct!");
				logger.error("File format is not correct!");
				return ret.toString();
			}
			if (!UploadFileUtils.validateSize(mf)) {
				ret.setCode(3);
				ret.setMessage("File size exceeds the allowable range!");
				logger.error("File size exceeds the allowable range!");
				return ret.toString();
			}
			UploadFile vo = UploadFileUtils.dealFilePath(request, userId, mf.getOriginalFilename());
			String realPath = UploadFileUtils.getRequestRealPath(request, vo.getRelativePath());
			File file = new File(realPath);
			mf.transferTo(file);

			// update to db...
			user.setPhoto(vo.getRealPath());
//			user.setUpdateBy(user.getId());
//			user.setUpdateDate(new Date());
			systemService.updateUserInfo(user);

			ret.setCode(0);
			ret.setMessage(Global.SUCCESS);
			ret.setObject(vo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			ret.setCode(10);
			ret.setMessage(Global.FAILED);
			e.printStackTrace();
		}
		System.out.println("return result=" + ret.toString());
		return ret.toString();
	}

	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public @ResponseBody String uploadFile(@RequestParam("userId") String userId, HttpServletRequest request) {
		ReturnObject ret = new ReturnObject();
		try {
			UploadFileUtils.uploadMutrilFile(userId, request);
		} catch (Exception e) {
			logger.error(e.getMessage());
			ret.setCode(10);
			ret.setMessage(Global.FAILED);
		}
		return ret.toString();
	}
}
